import AgoraRTC from 'agora-rtc-sdk'

import roomTabs from "@/components/roomTabs/roomTabs.vue";
import * as allUrls from '../../utils/allUrls'
import { videoPlayer } from 'vue-video-player'
import 'video.js/dist/video-js.css'
//设置直播参数
var rtc = {
	client: null,
	joined: false,
	published: false,
	localStream: null,
	remoteStreams: [],
	params: {},
};
//设置个人参数(默认)
var option = {
	appID: "de1f4fd7afd246f69e90dda83e7bb951",
	channel: "TextChannelName1",
	uid: "1235",
	token: "006de1f4fd7afd246f69e90dda83e7bb951IACtMq3scLcsSPtQ5nGcwCSfqZNeVmGcTJI/7BEfs1yrlq1XNx8AAAAAEAAe6RKypCWCYQEAAQCjJYJh"
};

export default {
	name: "liveRoom",
	components: {
		roomTabs,
		videoPlayer
	},
	data() {
		return {
			//发送的消息框内容
			chatInfo: '',
			//聊天内容总览
			chatScreenLive: [],
			screenLoading: true,
			sendInterval: false,
			//循环清弹幕
			displayInertval: '',
			//循环清除聊天池
			clearChatInfo: '',
			//弹幕
			barrage: [],
			//个人信息
			userInfo: {
				userType: ''
			},
			//主播信息
			hostInfo: '',
			//主播个人信息简介
			hostPersonalSum: '',
			isLive: 0, //判断是否已开播
			appid: "de1f4fd7afd246f69e90dda83e7bb951"
		}
	},
	methods: {
		//============主播创建直播客户端=============
		createHostLive(vors) {
			let _this = this;
			debugger
			//直播互动，建议模式为live,若为通信则为rtc
			rtc.client = AgoraRTC.createClient({
				mode: "live",
				codec: "h264"
			});
			//初始化
			rtc.client.init(option.appID, function() {
				console.log("客户端初始化完成");
				//设置角色=>"host"为主播,"audience"为观众
				rtc.client.setClientRole("host");
				//=======加入频道=======
				rtc.client.join("9a005a1f8e0347ec905f2f784cb27b9a", option.channel, option.uid, function(uid) {
					_this.$message.success("创建成功，欢迎您，" + uid);
					rtc.params.uid = uid;
/* 					AgoraRTC.getDevices(function(devices) {
						debugger
						console.log("first device id", devices[0].deviceId);
					}, function(err) {
						debugger
					}) */
					//角色为主播，发布本地流
					if(vors=="s"){
						rtc.localStream = AgoraRTC.createStream({
						streamID: rtc.params.uid,
						audio: true,
						video: false,
						screen: true,
					});
					}else{
						rtc.localStream = AgoraRTC.createStream({
							streamID: rtc.params.uid,
							audio: true,
							video: true,
							screen: false,
						});
					}
					//角色为主播，初始化本地流
					rtc.localStream.init(function() {
						debugger
						rtc.localStream.play('localStream2');
						console.log("初始化本地流成功！");
						_this.screenLoading = false;
						//发布
						rtc.client.publish(rtc.localStream, function(err) {
							_this.$message.error("发布本地流失败！");
							console.error(err);
						})
					}, function(err) {
						//检查是否有摄像头、麦
						_this.$message.error("初始化本地流失败，请检查摄像头和麦是否正常！")
						console.error("init local stream failed ", err);
					})
					
					
				}, function(err) {
					_this.$message.error("加入频道失败" + err);
					console.log(err)
				})
			}, (err) => {
				this.$message.error("直播间初始化失败，请检查授权码是否正确");
				console.error(err);
			});
		},

		//============观众加入直播间=================
		createAudLive() {
			let _this = this;
			//直播互动，建议模式为live,若为通信则为rtc
			rtc.client = AgoraRTC.createClient({
				mode: "live",
				codec: "h264"
			});
			//初始化
			rtc.client.init(option.appID, function() {
				console.log("客户端初始化完成");
				//设置角色=>"host"为主播,"audience"为观众
				rtc.client.setClientRole("audience");
				//加入频道
				rtc.client.join("9a005a1f8e0347ec905f2f784cb27b9a", option.channel, null, function(
					uid) {
					if (!_this.userInfo.username) {
						_this.$message.success("加入频道成功，欢迎您，" + uid);
					} else {
						_this.$message.success("加入频道成功，欢迎您，" + _this.userInfo.name);
					}
					rtc.params.uid = uid;
					//监听远程流
					rtc.client.on("stream-added", function(evt) {
						
						console.log("++++++++++监听到主播流")
						var remoteStream = evt.stream;
						
						var id = remoteStream.getId();
						if (id !== rtc.params.uid) {
							rtc.client.subscribe(remoteStream, function(err) {
								_this.$message.error("监听远程流失败" + err)
							})
						}
					});
					//监听远程订阅流
					rtc.client.on("stream-subscribed", function(evt) {
						console.log("++++++++++订阅主播流")
						var remoteStream = evt.stream;
						debugger
						console.log(remoteStream.play);
						var id = remoteStream.getId();
						// Add a view for the remote stream.
						// addView(id);
						// Play the remote stream.
						remoteStream.play('localStream');//绑定渲染
					})
				}, function(err) {
					_this.$message.error("加入频道失败" + err);
					console.log(err)
				})
			}, (err) => {
				this.$message.error(err);
				console.error(err);
			});
		},
		//离开直播间
		leaveLive() {
			debugger
			rtc.client.leave(function() {
				rtc.localStream.stop();
				rtc.localStream.close();
				while (rtc.remoteStreams.length > 0) {
					var stream = rtc.remoteStreams.shift();
					var id = stream.getId();
					stream.stop();
					removeView(id);
				}
				console.log("离开频道成功");
			}, function(err) {
				console.log("离开频道失败" + err);
			})
		},
		//获取是否开播状态
		getIsLiveStatus() {
			allUrls.getLiveStatus({
				username: this.hostInfo.username,
			}, 'post').then(res => {
				return res.json();
			}).then(res => {
				if (+res.status === 200) {
					this.isLive = res.data.isLive;
				} else {
					this.$message.error(res.desc);
				}
			}).catch(err => {
				console.log(err);
				this.$message.error("获取直播状态出错！");
			})
		},
	},
	beforeDestroy() {
		this.leaveLive();
		/*        clearInterval(this.displayInertval);
		        clearInterval(this.clearChatInfo); */
	},
	mounted() {

	},

}


